<?php

/*
 * This class is a base class and should not be used directly.
 * Extend your class with spAddListToListBase
 *
 */

//require_once "spGuid.php";

class spAddToListBase extends spDiv
{
	private $baseClassName;
	private $id;
	private $buttonAdd;

	public function __construct( $data=null )
	{
		$baseClassName = "spAddToListBase";
	
		parent::__construct( $data );
		parent::setClass( $baseClassName );
		
		$this->baseClassName = $baseClassName;		

		// create an id for this scope
		$this->id = spGuid::v4();
		$divId = new spDiv();
		$divId->setClass( $baseClassName."-id" );
		$divId->setContent( $this->id );
		$divId->addStyle( "display", "none" );
		parent::addChild( $divId );

		//////////////////////////////////
		// create insert coding
		//////////////////////////////////

		// container to make the clonable object invisible
		$divInsertContainer = new spDiv();
		$divInsertContainer->setClass( $baseClassName."-insertme" );
		$divInsertContainer->addStyle( "display", "none" );
		parent::addChild( $divInsertContainer );

		// this is the element being cloned (the class name is supplied so we can trash the item in the correct trashbin)
		$divInsertContainer->addChild( $this->createCloneableContainer() );

		//////////////////////////////////
		// add button
		//////////////////////////////////

		$this->buttonAdd = new spInputButton( array( "value"=>"Add", "name"=>"add") );
		parent::addChild( $this->buttonAdd );

		//////////////////////////////////
		// create table as a layout
		//////////////////////////////////

		$table = new spTable;
		$table->addStyle( "width", "100%" );
		parent::addChild( $table );

		$row = new spTableRow;
		$table->addChild( $row );

		// main container for list
		$containerList = new spTableData(array("name"=>"list"));
		$containerList->addStyle( "vertical-align", "top" );
		$row->addChild( $containerList );

		// trashcan
		$containerTrashcan = new spTableData(array("name"=>"trashcan"));
		$containerTrashcan->addStyle( "width", "90px" );
		$containerTrashcan->addStyle( "vertical-align", "bottom" );
		$containerTrashcan->addStyle( "text-align", "right" );
		$containerTrashcan->addStyle( "padding", "5px" );
		$row->addChild( $containerTrashcan );

		// extra container
		if ( isset($data["extra_container"]) )
		{
			$extraContainer = new spTableData;
			$extraContainer->addChild( $data["extra_container"] );
			$row->addChild( $extraContainer );
		}
		
		//////////////////////////////////
		// create list
		//////////////////////////////////

		$containerList->addChild( $divSortable = new spDiv(array("name_id"=>"insert_here","class"=>$baseClassName."-list")) );

		//////////////////////////////////
		// create trashbin
		//////////////////////////////////

		// fixme: we need to be wordpress free in the sigmaphp library
		$img = get_option('home').'/wp-content/plugins/gym/wpHelpers/trash.gif';
		$containerTrashcan->addChild( new spImage(array("src"=>$img, "class"=>$baseClassName."-trash")) );
	}

	public function getBaseClassName()
	{
		return $this->baseClassName;
	}

	protected function createCloneableContainer( $param=array() )
	{
		$divClonableElement = new spDiv();
		$divClonableElement->setClass( $this->baseClassName."-clonable-".$this->id );
		$divClonableElement->addChild( $this->createInsertHtml( $param ) );
		return $divClonableElement;
	}

	protected function createInsertHtml( $param=array() )
	{
		$div = new spDiv();
		$div->addStyle( "border", "black solid 1px" );
		$div->setContent( "Override the createInsertHtml define what you want inserted" );
		return $div;
	}
	
	public function render()
	{
		// some final tweaking
		// if readonly then we should remove the add button
		$data = $this->getData();
	
		$readonly = isset( $data["readonly"] )?$data["readonly"]:false;		
		
		if ( $readonly )
		{
			$this->removeChild( $this->buttonAdd );
		}
		
		parent::render();
	}
}

?>